package com.chj.chuji.tijie03;

import java.util.Arrays;

//https://blog.csdn.net/u014294166/article/details/50853634
//https://blog.csdn.net/li563868273/article/details/51306232
//https://blog.csdn.net/young_1004/article/details/83343485
public class FindShort {
	public static int findShortest(int[] A, int n) {
		if (A == null || n < 2)
			return 0;
		int[] temp = new int[n];
		for (int i = 0; i < n; i++) {
			temp[i] = A[i];
		}
		Arrays.sort(temp);
		int l = 0, r = n - 1;
		while (l < n && A[l] == temp[l]) {
			l++;
		}
		while (r > l && A[r] == temp[r]) {
			r--;
		}
		return r - l + 1;
	}

	public static int findShortest02(int[] A, int n) {
		int k = -1;
		int max = A[0];
		for (int i = 1; i < n; i++) {
			if (max > A[i])
				k = i;
			else
				max = A[i];
		}
		if (k == -1)
			return 0;
		int m = -1;
		int min = A[n - 1];
		for (int i = n - 2; i >= 0; i--) {
			if (min < A[i])
				m = i;
			else
				min = A[i];
		}
		System.out.println(k + " " + m);
		return k - m + 1;
	}

	public static void main(String[] args) {
		int[] A = new int[] { 1, 5, 3, 4, 2, 6, 7 };
		System.out.println(findShortest(A, 7));

		int[] A2 = new int[] { 1, 5, 3, 4, 2, 6, 7 };
		System.out.println(findShortest02(A2, 7));
	}
}
